package cn.b.sky.mes.mm.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.b.sky.mes.mm.model.Distribution;
import cn.b.sky.mes.mm.model.Requirement;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.b.sky.mes.mm.model.condition.DistributionCondition;
import cn.b.sky.mes.mm.model.condition.RequirementCondition;
import cn.b.sky.mes.mm.service.RequirementService;
import cn.b.sky.common.file.ExcelUtil;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;

/**
 * 
 * <strong>Title : RequirementStatusController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.controller<br></strong>
 * <strong>Description : </strong>需求配送状态统计控制器<br> 
 * <strong>Create on : 2016年9月22日 上午9:39:06<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/requirementStatus/*")
@Slf4j
public class RequirementStatusController extends SecurityController {
  @Autowired
  private RequirementService requirementService;

  @Autowired
  private ECodeService eCodeService;

  @RequestMapping("statusStatisticsPage")
  public String statusStatisticsPage(String navTabId, @ModelAttribute("condition") RequirementCondition condition,
      Model model, HttpSession session) {
    List<ECode> eCodes = eCodeService.selectECodeList(ECodeEnums.REQUIREMENT_STATUS.getCode(),
        SecurityContextUtil.getLanguageCode());
    condition.setSiteCode(getSiteCode());
    if (StringUtils.isEmpty(condition.getMinCreateDateTime())
        || StringUtils.isEmpty(condition.getMaxCreateDateTime())) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      condition.setMaxCreateDateTime(sdf.format(new Date()));
      Date date = new Date();
      date.setTime(date.getTime() - 7 * 24 * 60 * 60 * 1000);
      condition.setMinCreateDateTime(sdf.format(date));
    }
    Map<String, Integer> requirementStatusMap = requirementService.getRequirementStatus(condition, eCodes);
    model.addAttribute("statusECodes", eCodes);
    model.addAttribute("requirementStatusMap", requirementStatusMap);
    model.addAttribute("requirementStatusJson", JSON.toJSONString(requirementStatusMap));
    return "mm/requirement/distributionStatus";
  }

  @RequestMapping("viewRequirementsByStatus")
  public String viewRequirementsByStatus(@ModelAttribute("condition") RequirementCondition condition, Model model) {
    model.addAttribute("eCodes", JSON.toJSONString(
        eCodeService.selectECodeList(ECodeEnums.REQUIREMENT_STATUS.getCode(), SecurityContextUtil.getLanguageCode())));
    model.addAttribute("condition", JSON.toJSONString(condition));
    return "mm/requirement/groupRequirements";
  }

  @RequestMapping(value = "getRequirementsByStatus")
  @ResponseBody
  public DWZResult getRequirementsByStatus(RequirementCondition condition) {
    condition.setSiteCode(getSiteCode());
    if (StringUtils.isEmpty(condition.getStatusECode())) {
      return ajaxDone(requirementService.getOvertimeRequirementsByCondition(condition));
    }
    return ajaxDone(requirementService.getRequirementsByCondition(condition));
  }

  @SuppressWarnings("unchecked")
  @RequestMapping(value = "exportRequirementsToExcel")
  public void exportRequirementsToExcel(HttpServletResponse response, RequirementCondition condition, String colsMap) {
    condition.setSiteCode(this.getSiteCode());
    List<Requirement> requirements = new ArrayList<Requirement>();
    if (StringUtils.isEmpty(condition.getStatusECode())) {
      requirements = requirementService.getOvertimeRequirementsByCondition(condition).getData();
    } else {
      requirements = requirementService.getRequirementsByCondition(condition).getData();
    }
    try {
      ExcelUtil.exportToExcel(response,
          getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "requirementStatusStatistics.C"),
          ExcelUtil.parseColumnsStr2Map(colsMap), requirements);
    } catch (IOException e) {
      log.error("{}", e);
    }
  }

  @RequestMapping(value = "getDistributionsByRequirement")
  @ResponseBody
  public DWZResult getDistributionsByRequirement(DistributionCondition condition) {
    condition.setSiteCode(getSiteCode());
    return ajaxDone(requirementService.getDistributionsByRequirement(condition));
  }

  @RequestMapping(value = "exportDistributionsToExcel")
  public void exportDistributionsToExcel(HttpServletResponse response, String requirementNum, String colsMap) {
    List<Distribution> distributions = requirementService.getDistributionsByRequirement(getSiteCode(), requirementNum);
    try {
      ExcelUtil.exportToExcel(response,
          getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "requirementStatusStatistics.C"),
          ExcelUtil.parseColumnsStr2Map(colsMap), distributions);
    } catch (IOException e) {
      log.error("{}", e);
    }
  }

}
